package deque;

import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Map;

public class Solution_20 {
    public boolean isValid(String s) {
        Map<Character, Character> map = Map.of('{', '}', '(', ')', '[', ']');
        Deque<Character> deque = new ArrayDeque<>();
        for (char c : s.toCharArray()) {
            if (map.containsKey(c))
                deque.push(map.get(c));
            else if (deque.isEmpty() || deque.pop() != c)
                return false;
        }
        return deque.isEmpty();
    }
}
